module aprinter
import StdEnv, japi

/* This program shows how to print text and 
   draw figures like rectangles, circles,
   arcs, etc. In fact, printing is exactly
   like drawing. */

:: State = { fr::!F, canvas::!CP, quit::!E, print::!E}


Start world
  # (js, world) = begin_japi world
    (frm, js) = jnewFrame (XY 50 50) "Draw Things" js
    (cv, js) = jnewCanvas(XYWH 0 0 450 500) frm js
    (frm, js)= jpack frm js
    (mnb, js) = jnewBar frm js
    (filemn, js) = jnewMenu  "File" mnb js 
    (pr, js) = jnewItem  "Print" filemn js
    (qt, js) = jnewItem  "Quit" filemn js
    (frm, js) = jshow frm js
    s= { canvas= bg_color yellow cv, 
         quit= qt, print= pr, fr= frm}
    js = loop s js
  = end_japi js world
where
  loop :: !State !*JS -> *JS
  loop s js
      # (na,js) = jnextaction js
      |  na== (act s.quit)
                # js= jdispose s.canvas js
                = jquit js
      | na==(act s.print)
           # (prt, js)= jprinter s.fr js
             (w, prt)= jgetwidth prt
             (h, prt)= jgetheight prt
             wh= (toString w)+++", "+++(toString h)
             
             prt= jdraw (Txt (w-50, h-50) wh) prt
             prt= jprint(here prt (150, 200))
             js = jdispose prt js
             
             cv= jdraw (Txt (100, 80) wh) s.canvas
             cv= here cv (150,200)
           = loop {s & canvas= cv} js 
      = loop s js
      
  here :: !CP (!Int,!Int) -> CP
  here cv p=:(x,y)
      # cv= draw [ Fnt (Courier 20),
                   Txt p "Hello!",
                   Rct (x-20, y-30) 100 40,
                   Hue red,
                   Cir (x, y-5) 100] cv
      # cv= fill [ Hue blue,
                   Cir ((x+150),(y-30)) 50,
                   Rct p 50 80] cv
      # cv= jfilltriangle (20, 30) (20, 60) (80, 60) cv
      = color black cv 
   

 
          
          